﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class CateGoryService : ICategoryService
    {
        private ICategoryManager _ica;
        public CateGoryService(ICategoryManager ica)
        {
            _ica = ica;
        }
        public async Task<int> AddCategoryAsync(string title, int parentId)
        {
            return await _ica.AddAsync(new Model.Category 
            {
                Title = title,
                ParentId = parentId
            });
        }

        public async Task<int> DeleteCategoryAsync(int id)
        {
            var data = await _ica.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ica.DeleteAsync(data);
        }

        public async Task<List<CategoryDto>> GetAllCategoryAsync(int deleteId = 1)
        {
            var data = await _ica.GetChooseAsync(cg => cg.DeleteId == deleteId);
            return data == null ? new List<CategoryDto>() : data.Select(cg => new CategoryDto 
            {
                Title = cg.Title,
                ParentId = cg.ParentId
            }).ToList();
        }

        public async Task<CategoryDto> GetByIdAsync(int id)
        {
            var data = await _ica.GetById(id);
            return data == null ? new CategoryDto() : new CategoryDto
            {
                Id = data.Id,
                Title = data.Title,
                ParentId = data.ParentId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<List<CategoryDto>> GetByNameAsync(string title, int deleteId = 1)
        {
            var data = await _ica.GetChooseAsync(cg => cg.Title.Contains(title) && cg.DeleteId == deleteId);
            return data == null ? new List<CategoryDto>() : data.Select(cg => new CategoryDto 
            {
                Id = cg.Id,
                Title = cg.Title,
                ParentId = cg.ParentId,
                CreateTime = cg.CreateTime,
                UpdateTime = cg.UpdateTime
            }).ToList();
        }

        public async Task<List<CategoryDto>> GetByParentIdAsync(int parentId, int deleteId = 1)
        {
            var data = await _ica.GetChooseAsync(cg => cg.ParentId == parentId && cg.DeleteId == deleteId);
            return data == null ? new List<CategoryDto>() : data.Select(cg => new CategoryDto 
            {
                Id = cg.Id,
                Title = cg.Title,
                ParentId = cg.ParentId,
                CreateTime = cg.CreateTime,
                UpdateTime = cg.UpdateTime
            }).ToList();
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ica.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ica.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ica.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ica.UpdateAsync(data);
        }

        public async Task<int> UpdateCategoryAsync(int id, string title, int parentId)
        {
            var data = await _ica.GetById(id);
            if(data == null)
                return 0;
            data.Title = title;
            data.ParentId = parentId;
            return await _ica.UpdateAsync(data);
        }
    }
}
